package com.atguigu;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * QuartzTest
 *
 * @Author: 马伟奇
 * @CreateTime: 2021-01-30
 * @Description:
 */
public class QuartzTest {

    public static void main(String[] args) {
        // 创建调度器
        try {
            // Grab the Scheduler instance from the Factory
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

            // and start it off
            // 开始
            scheduler.start();

            // define the job and tie it to our HelloJob class
            // 构建工作对象，让工作对象不停的打印时间
            JobDetail job = JobBuilder.newJob(HelloJob.class)
                    // 给工作对象一个身份
                    .withIdentity("job1", "group1")
                    .build();

            // Trigger the job to run now, and then repeat every 40 seconds
            // 构建触发器对象 User user =   new User();
            Trigger trigger = TriggerBuilder.newTrigger()
                    // 给触发器对象一个身份
                    .withIdentity("trigger1", "group1")
                    // 现在开始执行
                    .startNow()
                    // 使用简单触发器
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                            // 每隔一秒钟执行一次
                            .withIntervalInSeconds(1)
                            // 重复执行
                            .repeatForever())
                    // 构建成功
                    .build();

            // Tell quartz to schedule the job using our trigger
            scheduler.scheduleJob(job, trigger);

            // 休眠6秒钟
            Thread.sleep(6000);

            // 结束
            scheduler.shutdown();

        } catch (Exception se) {
            se.printStackTrace();
        }
    }
}